package com.zxt.web.controller;

import com.zxt.service.UserService;
import com.zxt.service.impl.UserServiceImpl;
import com.zxt.util.Base64Utils;
import com.zxt.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * projectName:myXiaoMi
 * author:ZhangXiaoTeng
 * time:2021/12/4 9:53
 * description:
 */
@WebServlet("/activate")
public class ActivateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding("UTF-8");
        String email = request.getParameter("e");
        String code = request.getParameter("c");
        //校验
        if (StringUtils.isEmpty(email)||StringUtils.isEmpty(code)){
            request.setAttribute("msg","激活失败");
            request.getRequestDispatcher("/message.jsp").forward(request,response);
            return;
        }
        email= Base64Utils.decode(email);
        code=Base64Utils.decode(code);
        //进行激活
        UserService userService=new UserServiceImpl();
        try{
            userService.active(email,code);
            request.setAttribute("msg","激活成功");
            request.getRequestDispatcher("/message.jsp").forward(request,response);
        }catch (Exception e){
            request.setAttribute("msg","激活失败"+e.getMessage());
            request.getRequestDispatcher("/message.jsp").forward(request,response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

